package com.vip.jwt.model.wx.controller;

import java.sql.Time;
import java.text.DateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executor;

import cn.hutool.core.date.DateUtil;
import com.vip.jwt.common.entity.R;
import com.vip.jwt.common.utils.xcx.wx.WXAuthUtil;
import com.vip.jwt.config.ThreadPoolConfig;
import com.vip.jwt.model.wx.entity.Wxuser;
import com.vip.jwt.model.wx.service.WxuserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import lombok.extern.slf4j.Slf4j;
import sun.java2d.pipe.SpanIterator;

/**
 * @description: 微信用户列表 控制类
 * @author: yw
 * @time: 2021-09-03 11:14:27
 * @version: 1.0.0
 **/
@Api(tags = "微信用户列表控制类")
@Slf4j
@RestController
@RequestMapping("/api/wx/wxuser")
public class WxuserController {

    @Autowired
    private WxuserService wxuserService;

    @Autowired
    private Executor executor;

    /**
     * 同步微信用户信息
     * @return
     */
    @GetMapping("sysnWxUserList")
    public void sysnWxUserList(){
     wxuserService.sysnWxUserList();
    }



    @RequestMapping("sendInfo")
    public Object sendInfo(){
        String accessToken = WXAuthUtil.getAccessToken();
        Map<String, Object> map = new HashMap<>();
        Map<String,Object> data = new HashMap<>();
        data.put("value",111);
        data.put("color","#22475");
        map.put("data",data);
        WXAuthUtil.sendTemplateMessage(accessToken,"http://192.168.10.156:9999","rOcqC5Sv5MuhErBtK9KL4LBQ2Vwi1VQ-SUABCnqZwjA","om2-26CCeA6_ma_N-wKweaCyp89s",map);
        return R.ok();
    }

    /**
     * @Description 列表查询
     * @Author yw
     * @Date 2021-09-03 11:14:27
     * @Param
     * @return
     **/
    @ApiOperation("列表")
    @GetMapping("/list")
    public Object list(@RequestParam(required = false, defaultValue = "1") int current,
                          @RequestParam(required = false, defaultValue = "10") int size) {
        IPage<Wxuser> page = wxuserService.queryPage(current, size);
        return R.ok().data("page",page);
    }

    /**
     * @Description 根据id获取信息
     * @Author yw
     * @Date 2021-09-03 11:14:27
     * @Param openid 主键id
     * @return
     **/
    @ApiOperation("根据id获取信息")
    @GetMapping("/getById/{openid}")
    public Object getById(@PathVariable("openid") String openid) {
        for (int a=0;a<10;a++) {
            executor.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName());
                }
            });
            executor.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName());
                }
            });
        }
        return R.ok().data("data",wxuserService.getById(openid));
    }

    /**
     * @Description 保存
     * @Author yw
     * @Date 2021-09-03 11:14:27
     * @Param wxuser 详细信息
     * @return
     **/
    @ApiOperation("保存")
    @PostMapping("/save")
    public Object save(@RequestBody Wxuser wxuser) {
        return wxuserService.save(wxuser)?R.ok():R.error();
    }

    /**
     * @Description 修改
     * @Author yw
     * @Date 2021-09-03 11:14:27
     * @Param wxuser 详细信息
     * @return
     **/
    @ApiOperation("修改")
    @PutMapping("/updateById")
    public Object updateById(@RequestBody Wxuser wxuser) {
        return wxuserService.updateById(wxuser)?R.ok():R.error();
    }

    /**
     * @Description 删除
     * @Author yw
     * @Date 2021-09-03 11:14:27
     * @Param openid 主键id
     * @return
     **/
    @ApiOperation("删除")
    @DeleteMapping("/removeById/{openid}")
    public Object removeById(@PathVariable("openid") String openid) {
        return wxuserService.removeById(openid)?R.ok():R.error();
    }

    /**
     * @Description
     * @Author yw
     * @Date 2021-09-03 11:14:27
     * @Param openid 主键id
     * @return
     **/
    @ApiOperation("获取报警信息")
    @PostMapping("/WarningInfo")
    public Object WarningInfo(@RequestBody Map<String,Object> map) {
        map.keySet().stream().forEach(s->{
            System.out.println(s+"----------"+map.get(s));
        });
        for (int a=0;a<10;a++) {
            executor.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName());
                }
            });
            executor.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName());
                }
            });
        }


        return 0;
    }

    public static void main(String[] args) {
        ThreadPoolConfig threadPoolConfig=new ThreadPoolConfig();
        Executor asyncThread = threadPoolConfig.getAsyncThread();
        for (int a=0;a<10;a++){
            asyncThread.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName());
                }
            });
        };
    }
}
